<template>
  <div>
    <Form ref="form" :rules="formValidate" :label-width="90" :model="form">
      <FormItem label="整车_id" prop="vehicleId">
        <Input type="number" v-model.number="form.vehicleId" />
      </FormItem>
      <FormItem label="车身_id" prop="bodyId">
        <Input type="number" v-model.number="form.bodyId" />
      </FormItem>
      <FormItem label="发动机_id
" prop="engineId">
        <Input type="number" v-model.number="form.engineId" />
      </FormItem>
      <FormItem label="电机_id" prop="motorId">
        <Input type="number" v-model.number="form.motorId" />
      </FormItem>
      <FormItem label="电池_id" prop="batteryId">
        <Input type="number" v-model.number="form.batteryId" />
      </FormItem>
      <FormItem label="变速箱_id" prop="gearboxId">
        <Input type="number" v-model.number="form.gearboxId" />
      </FormItem>
      <FormItem label="变矩器_id" prop="torqueConvertId">
        <Input type="number" v-model.number="form.torqueConvertId" />
      </FormItem>
      <FormItem label="驱动桥_id" prop="transaxleId">
        <Input type="number" v-model.number="form.transaxleId" />
      </FormItem>
      <FormItem label="传动轴_id" prop="driveShaftId">
        <Input type="number" v-model.number="form.driveShaftId" />
      </FormItem>
      <FormItem label="轮胎_id" prop="wheelId">
        <Input type="number" v-model.number="form.wheelId" />
      </FormItem>
      <FormItem label="创建者" prop="createUser">
        <Input type="number" v-model.number="form.createUser" />
      </FormItem>
    </Form>
    <Row class="code-row-bg" justify="end" type="flex">
      <Button @click="cancel" style="margin-right:10px">取消</Button>
      <Button @click="save" type="primary">保存</Button>
    </Row>
  </div>
</template>
<script>
import { vehicleConfigurationApi } from "@/api/vehicle-configuration";
export default {
  name: "VehicleConfigurationListForm",
  components: {},
  props: {
    //是否为更新表单
    isUpdate: {
      type: Boolean,
      default: true
    },
    //更新的表单数据对象
    updateData: {
      type: Object
    }
  },
  data() {
    return {
      //表单数据
      form: {
        //整车_id
        vehicleId: null,
        //车身_id
        bodyId: null,
        //发动机_id

        engineId: null,
        //电机_id
        motorId: null,
        //电池_id
        batteryId: null,
        //变速箱_id
        gearboxId: null,
        //变矩器_id
        torqueConvertId: null,
        //驱动桥_id
        transaxleId: null,
        //传动轴_id
        driveShaftId: null,
        //轮胎_id
        wheelId: null,
        //创建者
        createUser: null
      },
      //表单验证
      formValidate: {
        //整车_id
        vehicleId: [
          {
            type: "number",
            required: true,
            message: "请输入整车_id",
            trigger: "blur"
          }
        ],
        //车身_id
        bodyId: [
          {
            type: "number",
            required: true,
            message: "请输入车身_id",
            trigger: "blur"
          }
        ],
        //发动机_id

        engineId: [
          {
            type: "number",
            required: true,
            message: "请输入发动机_id",
            trigger: "blur"
          }
        ],
        //电机_id
        motorId: [
          {
            type: "number",
            required: true,
            message: "请输入电机_id",
            trigger: "blur"
          }
        ],
        //电池_id
        batteryId: [
          {
            type: "number",
            required: true,
            message: "请输入电池_id",
            trigger: "blur"
          }
        ],
        //变速箱_id
        gearboxId: [
          {
            type: "number",
            required: true,
            message: "请输入变速箱_id",
            trigger: "blur"
          }
        ],
        //变矩器_id
        torqueConvertId: [
          {
            type: "number",
            required: true,
            message: "请输入变矩器_id",
            trigger: "blur"
          }
        ],
        //驱动桥_id
        transaxleId: [
          {
            type: "number",
            required: true,
            message: "请输入驱动桥_id",
            trigger: "blur"
          }
        ],
        //传动轴_id
        driveShaftId: [
          {
            type: "number",
            required: true,
            message: "请输入传动轴_id",
            trigger: "blur"
          }
        ],
        //轮胎_id
        wheelId: [
          {
            type: "number",
            required: true,
            message: "请输入轮胎_id",
            trigger: "blur"
          }
        ],
        //创建者
        createUser: [
          {
            type: "number",
            required: true,
            message: "请输入创建者",
            trigger: "blur"
          }
        ]
      }
    };
  },
  watch: {
    updateData: function (newValue, oldValue) {
      this.$refs["form"].resetFields();
      if (this.isUpdate) {
        for (let k in this.form) {
          this.$set(this.form, k, newValue[k]);
        }
        this.$set(this.form, "id", newValue["id"]);
      }
    },
    isUpdate: function (newValue, oldValue) {
      if (!newValue) {
        this.resetForm();
        this.$refs["form"].resetFields();
      }
    }
  },
  created() { },
  mounted() { },
  methods: {
    cancel() {
      this.resetForm();
      this.$emit("on-form-close");
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isUpdate) {
            this.update();
          } else {
            this.add();
          }
        } else {
          this.$Message.error("参数验证错误，请仔细填写表单数据!");
        }
      });
    },
    resetForm() {
      this.form = {
        //id
        id: null,
        //整车_id
        vehicleId: null,
        //车身_id
        bodyId: null,
        //发动机_id

        engineId: null,
        //电机_id
        motorId: null,
        //电池_id
        batteryId: null,
        //变速箱_id
        gearboxId: null,
        //变矩器_id
        torqueConvertId: null,
        //驱动桥_id
        transaxleId: null,
        //传动轴_id
        driveShaftId: null,
        //轮胎_id
        wheelId: null,
        //创建者
        createUser: null
      };
      this.$refs["form"].resetFields();
    },
    async add() {
      this.$Spin.show();
      let res = await vehicleConfigurationApi.addVehicleConfiguration(
        this.form
      );
      this.$Message.success(res.msg);
      this.$Spin.hide();
      this.resetForm();
      this.$emit("on-form-close");
      this.$emit("on-resetData");
    },
    async update() {
      this.$Spin.show();
      let res = await vehicleConfigurationApi.updateVehicleConfiguration(
        this.form
      );
      this.$Message.success(res.msg);
      this.$Spin.hide();
      this.resetForm();
      this.$emit("on-form-close");
      this.$emit("on-resetData");
    }
  }
};
</script>
